package client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.rmi.AccessException;
import java.security.cert.X509Certificate;

import javax.net.ssl.*;

public class Connection {
	private SSLSocket socket;
	private ObjectInputStream ois;
	private ObjectOutputStream oos;


	public boolean connect(String host) {
		int port = 34567;
		
		System.out.println("Connecting to " + host + ":" + port);
		try {
			socket = (SSLSocket)SSLSocketFactory.getDefault().createSocket(host, port);
			
			SSLSession session = socket.getSession();
			
			ois = new ObjectInputStream(socket.getInputStream());
			oos = new ObjectOutputStream(socket.getOutputStream());
			
			
			X509Certificate servercert = (X509Certificate)session.getPeerCertificates()[0];
//			System.out.println(servercert.getSubjectDN().getName());
			
			System.out.println("Connected");
			return true;		
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return false;
	}
	
	public void disconnect(){
		try {
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getList() throws AccessException {
		send("list");
		return receive();
	}

	public String viewJournal(String journal) throws AccessException {
		send("get");
		send(journal);
		return receive();
	}


	public void editJournal(String journal) throws AccessException {
		send("list");
		send(journal);
		receive();
	}

	public void createJournal(String patient, String nurse) throws AccessException {
		send("create");
		send(patient);
		send(nurse);
		receive();
	}

	public void deleteJournal(int id) throws AccessException {
		send("delete");
		send(id);
		receive();
	}	
	
	private void send(Serializable s){
		
	}

	private String receive() throws AccessException {
		int status = 0;
		String response = "derp";
		if(status != 0){
			throw new AccessException("Permission denied");
		}
		return response;
	}

	

}
